package com.mytx.mapper;

import com.mytx.pojo.Topic;
import com.mytx.pojo.TopicExample;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public interface TopicMapper {


    /**
     * 根据用户id 查询没有删除的主题
     *
     * @param uId
     * @return
     */
    List<Topic> selectMyTopic(String uId);

    /**
     * 根据主题id查询出主题信息以及管理员信息
     * @param topicId
     * @return
     */
    Topic selectTopicByTopicId(String topicId);

    /**
     * 根据主题id 查询完整的主题，包含发布者个人信息
     *
     * @param topicId
     * @return
     */
    Topic selectFullTopicByPrimaryKey(String topicId);

    /**
     * 将管理员id设置为null
     * @param topicId
     * @return
     */
    @Update("UPDATE topic SET manager = NULL WHERE id = #{topicId}")
    int updateManagerNullByTopicId(String topicId);

    /**
     * 增加主题的评论数量
     *
     * @param topicId
     * @return
     */
    @Select(" UPDATE topic  SET comment_size = @value:= comment_size + 1 WHERE id = #{topicId}; SELECT comment_size FROM topic WHERE id = #{topicId} ")
    int incrementCommentSize(String topicId);

    /**
     * 增加悬赏类型主题的 悬赏任务领取数量
     *
     * @param topicId
     * @return
     */
    @Select(" UPDATE topic  SET receive_task_size = receive_task_size + 1 WHERE id = #{topicId}; SELECT receive_task_size FROM topic WHERE id = #{topicId} ")
    int incrementReceiveTaskSize(String topicId);

    /**
     * 增加主题收到的
     * 捐赠总额
     * 捐赠余额
     * <p>
     * 返回捐赠余额、捐赠总额、笔数
     *
     * @param increment
     * @param topicId
     */
    //@ResultType(Topic.class)
    @ResultMap("customResultMap")
    @Select(" UPDATE topic  SET donation_balance = @value:= donation_balance + #{increment},donation_received =@value:= donation_received + #{increment} ,updated= #{updated}  WHERE id = #{topicId}; SELECT id,donation_balance ,donation_received,donation_size FROM topic WHERE id = #{topicId}")
    Topic incrementReceivedDonationData(@Param("increment") BigDecimal increment, @Param("topicId") String topicId, @Param("updated") Date updated);

    /**
     * 增加主题收到的
     * 捐赠总额
     * 捐赠余额
     * 捐赠数量
     * <p>
     * 返回捐赠余额、捐赠总额、笔数
     *
     * @param increment
     * @param topicId
     */
    //@ResultType(Topic.class)
    @ResultMap("customResultMap")
    @Select(" UPDATE topic  SET donation_balance = @value:= donation_balance + #{increment},donation_received =@value:= donation_received + #{increment},donation_size=donation_size+1 ,updated= #{updated}  WHERE id = #{topicId}; SELECT id,donation_balance ,donation_received,donation_size FROM topic WHERE id = #{topicId}")
    Topic incrementReceivedDonationData_v1(@Param("increment") BigDecimal increment, @Param("topicId") String topicId, @Param("updated") Date updated);

    /**
     * 减少主题捐赠余额和捐赠总额
     *
     * @param amount
     * @param topicId
     * @return
     */
    @ResultMap("customResultMap")
    @Select(" UPDATE topic  SET donation_balance =  donation_balance - #{increment},donation_received = donation_received - #{increment},updated=#{updated} WHERE id = #{topicId}; SELECT id,donation_balance ,donation_received,donation_size FROM topic WHERE id = #{topicId}")
    Topic reduceDonationBalanceAndDonationReceived(@Param("increment") BigDecimal amount, @Param("topicId") String topicId, @Param("updated") Date updated);

    /**
     * 用于转发捐赠
     * 减少主题捐赠余额
     * 增加 转发次数
     *
     * @param reduceAmount
     * @param topicId
     * @return
     */
    @ResultMap("customResultMap")
    @Select("UPDATE topic  SET donation_balance =  donation_balance - #{reduceAmount},forward_size= forward_size+1 WHERE id = #{topicId};SELECT id, donation_balance,forward_size  FROM topic WHERE id = #{topicId}")
    Topic reduceDonationBalance(@Param("reduceAmount") BigDecimal reduceAmount, @Param("topicId") String topicId);

    /**
     * 用于转发捐赠，并对捐赠进行分拆的情况
     * 减少主题捐赠余额
     * 增加 转发次数
     * 增加 捐赠数量
     *
     * @param reduceAmount
     * @param topicId
     * @return
     */
    @ResultMap("customResultMap")
    @Select("UPDATE topic  SET donation_balance =  donation_balance - #{reduceAmount},forward_size= forward_size+1 ,donation_size = donation_size+1   WHERE id = #{topicId};SELECT id, donation_balance,forward_size  FROM topic WHERE id = #{topicId}")
    Topic reduceDonationBalance_V1(@Param("reduceAmount") BigDecimal reduceAmount, @Param("topicId") String topicId);

    //----------------------------------------------------分割线------------------------------------------------

    int countByExample(TopicExample example);

    int deleteByExample(TopicExample example);

    int deleteByPrimaryKey(String id);

    int insert(Topic record);

    int insertSelective(Topic record);

    List<Topic> selectByExample(TopicExample example);

    Topic selectByPrimaryKey(String id);

    int updateByExampleSelective(@Param("record") Topic record, @Param("example") TopicExample example);

    int updateByExample(@Param("record") Topic record, @Param("example") TopicExample example);

    int updateByPrimaryKeySelective(Topic record);

    int updateByPrimaryKey(Topic record);

}